{% extends 'frontend/common/basic_form.html' %}

{% block script_content %}

    <script>
        {% ifequal plugin_result.is_add True %}
            $('#id_cabinet').empty();
            $("#id_cabinet").append("<option selected='selected' value = ''>请选择机房</option>");
            $("#id_rack").append("<option selected='selected' value = ''>请选择机柜</option>");
        {% endifequal %}
        {% ifequal plugin_result.is_add False%}
            $('#id_cabinet').empty();
            {% for cabinet in plugin_result.cabinet_list %}
                {% ifequal cabinet entity.cabinet.id %}
                    $("#id_cabinet").append("<option selected='selected' value='{{ cabinet.id }}'>{{ cabinet.name }}</option>");
                {% endifequal %}
                {% ifnotequal  cabinet entity.cabinet.id  %}
                    $("#id_cabinet").append("<option value='{{ cabinet.id }}'>{{ cabinet.name }}</option>");
                {% endifnotequal %}
            {% endfor %}
        {% endifequal %}
        $('#id_rack').empty();
        {% for rack in plugin_result.rack_list %}
            {% ifequal rack entity.rack.id %}
                $("#id_rack").append("<option selected='selected' value='{{ rack.id }}'>{{ rack.name }}</option>");
            {% endifequal %}
            {% ifnotequal  rack entity.rack.id  %}
                $("#id_rack").append("<option value='{{ rack.id }}'>{{ rack.name }}</option>");
            {% endifnotequal %}
        {% endfor %}
        function loadCabinet() {
            if ($('#id_idc').val() != '') {
                $.get('/frontend/cmdb/rack_list/' + $('#id_idc').val() + '/load_cabinet_list/', function (rs) {
                    optionList = eval(rs)
                    $('#id_cabinet').empty()
                    for (var i = 0; i < optionList.length; i++) {
                        $("#id_cabinet").append("<option value=" + optionList[i].pk + ">" + optionList[i].fields.name + "</option>");
                    }
                    loadRack()
                })
            } else {
                $('#id_cabinet').empty()
                $("#id_cabinet").append("<option value=''>请选择机房</option>");
                $('#id_rack').empty()
                $("#id_rack").append("<option value=''>请选择机柜</option>");
            }
        }
        $('#id_idc').change(function () {
            loadCabinet()
        })

        function loadRack() {
            if ($('#id_idc').val() != '' && $('#id_cabinet').val()) {
                $.get('/frontend/cmdb/rack_list/' + $('#id_idc').val() + '/' + $('#id_cabinet').val() + '/load_rack_list/', function (rs) {
                    optionList = eval(rs)
                    $('#id_rack').empty()
                    for (var i = 0; i < optionList.length; i++) {
                        $("#id_rack").append("<option value=" + optionList[i].pk + ">" + optionList[i].fields.name + "</option>");
                    }
                })
            } else {
                $('#id_rack').empty()
                $("#id_rack").append("<option value=''>请选择机柜</option>");
            }
        }
        $('#id_cabinet').change(function () {
            loadRack()
        })
    </script>

{% endblock %}
